package com.ph.irp.base.usermanage.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.ph.irp.base.usermanage.dao.PBasePurviewFunctionDAO;
import com.ph.irp.model.PBasePurviewFunction;
import com.ph.util.DBUtil;

public class PBasePurviewFunctionDAOImpl implements PBasePurviewFunctionDAO {
	private ComboPooledDataSource dataSource;


	public void setDataSource(ComboPooledDataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	public PBasePurviewFunction getFunctionByFunctionID(int functionID)throws Throwable{
		PBasePurviewFunction result = new PBasePurviewFunction();

		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		try {
			StringBuilder sb = new StringBuilder();

			sb.append("select f.FunctionName functionname,f.FunctionTypeID functiontypeid ");
			sb.append("from p_base_purview_function f ");
			sb.append("where FunctionID=").append(functionID);
			con = dataSource.getConnection();

			pStmt = con.prepareStatement(sb.toString());
			rs = pStmt.executeQuery();

			while (rs.next()) {
				result.setFunctionName(rs.getString("functionname"));
				result.setPBasePurviewFunctiontypeId(rs.getInt("functiontypeid"));
			}
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			try {
				DBUtil.setNullObject(con, pStmt, rs);
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}

		return result;
	
		
	}
	
	public List<PBasePurviewFunction> getFunctionByFunctionTypeID(int functionTypeID)throws Throwable{
		List<PBasePurviewFunction> resList = new ArrayList<PBasePurviewFunction>();
		

		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		try {
			StringBuilder sb = new StringBuilder();

			sb.append("select f.FunctionName functionname,f.FunctionTypeID functiontypeid,f.FunctionDesc fDesc ");
			sb.append("from p_base_purview_function f ");
			sb.append("where FunctionTypeID=").append(functionTypeID);
			con = dataSource.getConnection();

			pStmt = con.prepareStatement(sb.toString());
			rs = pStmt.executeQuery();

			while (rs.next()) {
				PBasePurviewFunction result = new PBasePurviewFunction();
				result.setFunctionDesc(rs.getString("fDesc"));
				result.setFunctionName(rs.getString("functionname"));
				result.setPBasePurviewFunctiontypeId(rs.getInt("functiontypeid"));
				resList.add(result);
			}
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			try {
				DBUtil.setNullObject(con, pStmt, rs);
			} catch (Exception e) {
				throw new Exception(e.getMessage());
			}
		}

		return resList;
	}
}
